{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "880be189-794e-4873-837f-a8353893ab2f",
   "metadata": {},
   "outputs": [],
   "source": [
    "class BillRecord:\n",
    "    def __init__(self, date, amount, category, note):\n",
    "        self.date = date\n",
    "        self.amount = amount\n",
    "        self.category = category\n",
    "        self.note = note"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "15c24a5e-56b0-4af0-ae38-c2d8a23d0585",
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_menu():\n",
    "    print(\"=================================\")\n",
    "    print(\"欢迎使用个人账单管理系统\")\n",
    "    print(\"=================================\")\n",
    "    print(\"1. 记录收入\")\n",
    "    print(\"2. 记录支出\")\n",
    "    print(\"3. 查看所有账单\")\n",
    "    print(\"4. 查询账单\")\n",
    "    print(\"5. 退出系统\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "011c4866-54b5-4012-9182-0ed5858986a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "def record_income():\n",
    "    date = input(\"请输入收入日期（格式：YYYY-MM-DD）：\")\n",
    "    amount = float(input(\"请输入收入金额：\"))\n",
    "    if amount <= 0:\n",
    "        print(\"收入金额必须为正数。\")\n",
    "        return\n",
    "    category = input(\"请输入收入类别（如工资、奖金等）：\")\n",
    "    note = input(\"请输入备注：\")\n",
    "    record = BillRecord(date, amount, category, note)\n",
    "    income_records.append(record)\n",
    "    print(\"收入记录成功。\")\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "9389a404-29fd-4296-a6bf-4c38a192da1b",
   "metadata": {},
   "outputs": [],
   "source": [
    "def record_expense():\n",
    "    date = input(\"请输入支出日期（格式：YYYY-MM-DD）：\")\n",
    "    amount = float(input(\"请输入支出金额：\"))\n",
    "    if amount <= 0:\n",
    "        print(\"支出金额必须为正数。\")\n",
    "        return\n",
    "    category = input(\"请输入支出类别（如餐饮、交通、购物等）：\")\n",
    "    note = input(\"请输入备注：\")\n",
    "    record = BillRecord(date, amount, category, note)\n",
    "    expense_records.append(record)\n",
    "    print(\"支出记录成功。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "4e0cb6de-9dfe-4330-8986-793137b37cfa",
   "metadata": {},
   "outputs": [],
   "source": [
    "def show_all_bills():\n",
    "    print(\"收入账单:\")\n",
    "    for record in income_records:\n",
    "        print(f\"日期: {record.date}, 金额: {record.amount}, 类别: {record.category}, 备注: {record.note}\")\n",
    "    print(\"支出账单:\")\n",
    "    for record in expense_records:\n",
    "        print(f\"日期: {record.date}, 金额: {record.amount}, 类别: {record.category}, 备注: {record.note}\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "fc3fd32c-5504-49b0-bffd-b5a1fe5a3562",
   "metadata": {},
   "outputs": [],
   "source": [
    "def query_bills(income_records, expense_records):\n",
    "    print(\"1. 按日期查询\")\n",
    "    print(\"2. 按日期范围查询\")\n",
    "    print(\"3. 按类别查询\")\n",
    "    option = input(\"请选择查询方式: \")\n",
    "    if option == \"1\":\n",
    "        date = input(\"请输入日期 (YYYY-MM-DD): \")\n",
    "        print(\"收入账单:\")\n",
    "        for income in income_records:\n",
    "            if income.date == date:\n",
    "                print(f\"收入: 日期: {income.date}, 金额: {income.amount}, 类别: {income.category}, 备注: {income.note}\")\n",
    "        print(\"支出账单:\")\n",
    "        for expense in expense_records:\n",
    "            if expense.date == date:\n",
    "                print(f\"支出: 日期: {expense.date}, 金额: {expense.amount}, 类别: {expense.category}, 备注: {expense.note}\")\n",
    "    elif option == \"2\":\n",
    "        start_date = input(\"请输入开始日期 (YYYY-MM-DD): \")\n",
    "        end_date = input(\"请输入结束日期 (YYYY-MM-DD): \")\n",
    "        print(\"收入账单:\")\n",
    "        for income in income_records:\n",
    "            if start_date <= income.date <= end_date:\n",
    "                print(f\"日期: {income.date}, 金额: {income.amount}, 类别: {income.category}, 备注: {income.note}\")\n",
    "        print(\"支出账单:\")\n",
    "        for expense in expense_records:\n",
    "            if start_date <= expense.date <= end_date:\n",
    "                print(f\"日期: {expense.date}, 金额: {expense.amount}, 类别: {expense.category}, 备注: {expense.note}\")\n",
    "    elif option == \"3\":\n",
    "        category = input(\"请输入类别: \")\n",
    "        print(f\"正在查询类别为 {category} 的账单\")  # 调试语句，打印查询类别\n",
    "        print(\"收入账单:\")\n",
    "        print(f\"当前收入记录: {income_records}\")  # 调试语句，打印收入记录列表\n",
    "        for income in income_records:\n",
    "            if income.category == category:\n",
    "                print(f\"收入: 日期: {income.date}, 金额: {income.amount}, 类别: {income.category}, 备注: {income.note}\")\n",
    "        print(\"支出账单:\")\n",
    "        print(f\"当前支出记录: {expense_records}\")  # 调试语句，打印支出记录列表\n",
    "        for expense in expense_records:\n",
    "            if expense.category == category:\n",
    "                print(f\"支出: 日期: {expense.date}, 金额: {expense.amount}, 类别: {expense.category}, 备注: {expense.note}\")\n",
    "    else:\n",
    "        print(\"无效的查询选项。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "id": "b22d021a-689e-4531-86f3-5f6ea027996a",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "=================================\n",
      "欢迎使用个人账单管理系统\n",
      "=================================\n",
      "1. 记录收入\n",
      "2. 记录支出\n",
      "3. 查看所有账单\n",
      "4. 查询账单\n",
      "5. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  1\n",
      "请输入收入日期（格式：YYYY-MM-DD）： 2024-11-12\n",
      "请输入收入金额： 1000\n",
      "请输入收入类别（如工资、奖金等）： 工资\n",
      "请输入备注： 无\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "收入记录成功。\n",
      "=================================\n",
      "欢迎使用个人账单管理系统\n",
      "=================================\n",
      "1. 记录收入\n",
      "2. 记录支出\n",
      "3. 查看所有账单\n",
      "4. 查询账单\n",
      "5. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  2\n",
      "请输入支出日期（格式：YYYY-MM-DD）： 2024-11-22\n",
      "请输入支出金额： 188\n",
      "请输入支出类别（如餐饮、交通、购物等）： 购物\n",
      "请输入备注： 无\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "支出记录成功。\n",
      "=================================\n",
      "欢迎使用个人账单管理系统\n",
      "=================================\n",
      "1. 记录收入\n",
      "2. 记录支出\n",
      "3. 查看所有账单\n",
      "4. 查询账单\n",
      "5. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  3\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "收入账单:\n",
      "日期: 2024-11-12, 金额: 1000.0, 类别: 工资, 备注: 无\n",
      "支出账单:\n",
      "日期: 2024-11-22, 金额: 188.0, 类别: 购物, 备注: 无\n",
      "=================================\n",
      "欢迎使用个人账单管理系统\n",
      "=================================\n",
      "1. 记录收入\n",
      "2. 记录支出\n",
      "3. 查看所有账单\n",
      "4. 查询账单\n",
      "5. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. 按日期查询\n",
      "2. 按日期范围查询\n",
      "3. 按类别查询\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择查询方式:  1\n",
      "请输入日期 (YYYY-MM-DD):  2024-11-12\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "收入账单:\n",
      "收入: 日期: 2024-11-12, 金额: 1000.0, 类别: 工资, 备注: 无\n",
      "支出账单:\n",
      "=================================\n",
      "欢迎使用个人账单管理系统\n",
      "=================================\n",
      "1. 记录收入\n",
      "2. 记录支出\n",
      "3. 查看所有账单\n",
      "4. 查询账单\n",
      "5. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. 按日期查询\n",
      "2. 按日期范围查询\n",
      "3. 按类别查询\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择查询方式:  2\n",
      "请输入开始日期 (YYYY-MM-DD):  2024-11-01\n",
      "请输入结束日期 (YYYY-MM-DD):  2024-11-23\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "收入账单:\n",
      "日期: 2024-11-12, 金额: 1000.0, 类别: 工资, 备注: 无\n",
      "支出账单:\n",
      "日期: 2024-11-22, 金额: 188.0, 类别: 购物, 备注: 无\n",
      "=================================\n",
      "欢迎使用个人账单管理系统\n",
      "=================================\n",
      "1. 记录收入\n",
      "2. 记录支出\n",
      "3. 查看所有账单\n",
      "4. 查询账单\n",
      "5. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  4\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1. 按日期查询\n",
      "2. 按日期范围查询\n",
      "3. 按类别查询\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请选择查询方式:  3\n",
      "请输入类别:  工资\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "正在查询类别为 工资 的账单\n",
      "收入账单:\n",
      "当前收入记录: [<__main__.BillRecord object at 0x000001DC0FFBE620>]\n",
      "收入: 日期: 2024-11-12, 金额: 1000.0, 类别: 工资, 备注: 无\n",
      "支出账单:\n",
      "当前支出记录: [<__main__.BillRecord object at 0x000001DC0FFBE470>]\n",
      "=================================\n",
      "欢迎使用个人账单管理系统\n",
      "=================================\n",
      "1. 记录收入\n",
      "2. 记录支出\n",
      "3. 查看所有账单\n",
      "4. 查询账单\n",
      "5. 退出系统\n"
     ]
    },
    {
     "name": "stdin",
     "output_type": "stream",
     "text": [
      "请输入选项序号:  5\n"
     ]
    },
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "感谢使用，程序已退出。\n"
     ]
    }
   ],
   "source": [
    "if __name__ == \"__main__\":\n",
    "    income_records = []\n",
    "    expense_records = []\n",
    "    while True:\n",
    "        show_menu()\n",
    "        choice = input(\"请输入选项序号: \")\n",
    "        if choice == \"1\":\n",
    "            record_income()\n",
    "        elif choice == \"2\":\n",
    "            record_expense()\n",
    "        elif choice == \"3\":\n",
    "            show_all_bills()\n",
    "        elif choice == \"4\":\n",
    "            query_bills(income_records, expense_records)\n",
    "        elif choice == \"5\":\n",
    "            print(\"感谢使用，程序已退出。\")\n",
    "            break\n",
    "        else:\n",
    "            print(\"无效的选项序号，请重新输入。\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "04e63384-86aa-4dc4-8517-ea481f68338e",
   "metadata": {},
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "950b7f56-88b9-467c-9dd0-3c32b73d4950",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.10.11"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
